<template>
  <div class="list_box" v-show="status">
    <div class="list" @mousemove="movelist" @mouseleave="leavelist">
      <div class="loaction">
        <div class="i1">
          <span>搜索记录</span>
          <i class="el-icon-delete"></i>
        </div>

        <ul class="ul1 clear">
          <li v-for="(val, index) in searchLoaction" :key="index" @click.stop="searchLoactionValue(val)" @mousemove="mousemoveDelect(index)">
            <span>{{val}}</span>
            <i :id="deleteId == index ? 'delete1' : ''" class="el-icon-close" @click.stop="deletehandle(index)"></i>
          </li>
        </ul>
      </div>

      <div class="hot_music">
        <div class="hot">热搜榜</div>
        <ul class="ul2">
          <li v-for="(val, index) in searchHotList" :key="index" @click="hotSong(val.searchWord)">
            <span :class="index < 9 ? 'p1' : ''" :id="index < 3 ? 'p1' : ''">{{
              index + 1
            }}</span>
            <a href="#">{{ val.searchWord }}</a>
            <span class="score">{{ val.score }}</span>
            <span
              ><img v-if="val.iconUrl != null" :src="val.iconUrl" alt=""
            /></span>
          </li>
        </ul>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  data () {
    return {
      status: false,
      listState: false,
      searchHotList: [], // 热门榜单
      searchLoaction: [], // 本地搜索记录
      deleteId: -1
    }
  },
  mounted () {
    // 获取本地搜索记录
    this.searchLoaction = JSON.parse(localStorage.getItem('searchLoaction')) || []
    this.$bus.$on('refresh', () => {
      this.searchLoaction = JSON.parse(localStorage.getItem('searchLoaction')) || []
    })

    // 当input获取焦点显示搜索列表
    this.$bus.$on('inpFocus', (data) => {
      this.getapi.searchHot().then((res) => {
        this.searchHotList = res.data
        this.status = true
        console.log(res.data)
      })
    })
    // 当input失去焦点隐藏搜索列表
    this.$bus.$on('inpBlur', (data) => {
      if (!this.listState) {
        this.status = false
        document.removeEventListener('click', this.isShow)
      }
    })
    this.$bus.$on('handle', (data) => {
      this.status = true
    })
  },
  methods: {
    movelist () {
      this.listState = true
      console.log(this.listState)
    },
    leavelist () {
      this.listState = false
      if (this.status) {
        document.addEventListener('click', this.isShow)
      }
    },
    isShow () {
      this.status = false
      // console.log("你好")
      document.removeEventListener('click', this.isShow)
    },
    hotSong (e) {
      console.log(e, 1234567890)
      this.$bus.$emit('searchResult', this.searchValue)
      // 跳转搜索结果展示页面
      this.$router.push({
        path: '/search',
        query: {
          name: e
        }
      })
      // 关闭搜索列表
      this.status = false
    },

    searchLoactionValue (e) {
      this.$bus.$emit('searchResult', e)
      // 跳转搜索结果展示页面
      this.$router.push({
        path: '/search',
        query: {
          name: e
        }
      })
      // 关闭搜索列表
      this.status = false
    },
    mousemoveDelect (index) {
      this.deleteId = index
      // console.log(this.deleteId);
    },
    deletehandle (index) {
      console.log(index)
      const arr = JSON.parse(localStorage.getItem('searchLoaction'))
      arr.splice(index, 1)
      localStorage.setItem('searchLoaction', JSON.stringify(arr))
      this.searchLoaction = JSON.parse(localStorage.getItem('searchLoaction')) || []
      console.log(JSON.parse(localStorage.getItem('searchLoaction')), 123)
    }

  }
}
</script>

<style scoped>
.list_box {
  width: 350px;
  height: 500px;
  position: absolute;
  background-color: rgb(255, 255, 255);
  z-index: 99;
  top: 40px;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 5px;
  box-shadow: 0 0 1px 1px #e1e1e1;
  overflow-y: scroll;
}
.list_box::-webkit-scrollbar {
  width: 5px;
}
.list_box::-webkit-scrollbar-thumb {
  background-color: #d8d8d8;
  border-radius: 10px;
}

.list {
  width: 95%;
  height: auto;
  margin: 0 auto;
}
.loaction .i1 {
  font-size: 1.2rem;
  padding: 10px;
}
.i1 i {
  margin-left: 5px;
  font-size: 1rem;
}
.loaction .ul1 li {
  width: auto;
  height: auto;
  background-color: white;
  float: left;
  box-sizing: border-box;
  margin-top: 5px;
  margin-left: 5px;
  padding: 5px;
  border-radius: 15px;
  border: 1px solid #d8d8d8;
  font-size: 0.7rem;
  padding: 5px 10px;
  cursor: pointer;
}
.loaction .ul1 i{
  margin-left: 5px;
  display: none;
}
.ul1 #delete1{
  display: inline-block;
}
.hot_music {
  margin-left: 5px;
}
.hot_music li {
  width: 100%;
  line-height: 70px;
  margin-bottom: 1px;
}
.hot_music .hot {
  width: 100%;
  line-height: 70px;
  font-size: 1rem;
  color: #797979;
}
.hot_music span {
  margin-right: 10px;
  font-size: 1rem;
}
.hot_music a {
  font-size: 1rem;
  color: black;
}
.hot_music #p1 {
  color: #ff3a3a;
}
#p1 ~ a {
  font-weight: 700;
}
.hot_music .p1 {
  margin-right: 18px;
}
.hot_music .score {
  margin: auto 10px;
  color: #cccccc;
  font-size: 1rem;
}
.hot_music li img {
  width: 20px;
  height: 10px;
}
</style>
